package com.dingwen.treasure.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dingwen.treasure.base.controller.BaseViewController;
import com.dingwen.treasure.base.pojo.vo.PageVo;
import com.dingwen.treasure.base.pojo.vo.Result;
import com.dingwen.treasure.model.entity.mybatisplus.SysUser;
import com.dingwen.treasure.mybatisplus.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Mybatis Plus控制器 </br>
 * <ul>
 *     <li>分页原理： 使用拦截器获取 ThreadLocal 中的 RowBounds 进行逻辑分页，大量的数据可能导致OOM</li>
 *     <li>逻辑分页实现： 每次分页查询都到数据库执行分页查询 （每种数据库的分页实现不一致）</li>
 *     <li>TODO 分页优化：大数据量查询时先查询ID在进行关联 （MYSQL）</li>
 * </ul>
 *
 * @author dingwen
 * @date 2022/05/03
 */
@Api(tags = "mybatis plus")
@Slf4j
@RestController
@RequestMapping("mybatis/plus")
public class MybatisPlusController implements BaseViewController {

    /**
     * 系统用户服务
     */
    @Resource
    private SysUserService sysUserService;

    /**
     * 通过用户id查询单个用户
     *
     * @return {@link Result}
     */
    @ApiOperation("通过用户id查询单个用户")
    @GetMapping("/users/{id}")
    public Result<SysUser> findById(@PathVariable Long id) {
        return success(sysUserService.getById(id));
    }

    @ApiOperation("分页查询")
    @GetMapping("/users-page")
    public Result<PageVo<SysUser>> findPage(SysUser sysUser) {
        startPage();
        return page(sysUserService.list(new LambdaQueryWrapper<>(sysUser)));
    }
}
